<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="车列号" prop="carCode">
        <el-input
          v-model="queryParams.carCode"
          placeholder="请输入车列号"
          style="width: 220px"
          maxlength="15"
          show-word-limit
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="作业类型">
        <el-select
          clearable
          v-model="queryParams.type"
          placeholder="请选择类型"
          style="width: 220px"
        >
          <el-option
            v-for="dict in types"
            :key="dict.id"
            :label="dict.name"
            :value="dict.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="作业时间">
        <el-date-picker
          v-model="dateRange"
          style="width: 220px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="班组" prop="workTeamName">
        <el-input
          v-model="queryParams.workTeamName"
          placeholder="请输入班组"
          clearable
          style="width: 220px"
          maxlength="10"
          show-word-limit
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="股道" prop="trackName">
        <el-input
          v-model="queryParams.trackName"
          placeholder="请输入股道"
          clearable
          style="width: 220px"
          maxlength="10"
          show-word-limit
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <div class="searchReset">
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery"
            >搜索</el-button
          >
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
            >重置</el-button
          >
        </div>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <!-- <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:record:add']"
        >新增</el-button>
      </el-col>

      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:record:remove']"
        >删除</el-button>
      </el-col> -->

      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <div :class="eleTabTop <= 85 ? 'eleTab' : ''" class="informationTab" ref="eleTab">
      <el-table
        v-loading="loading"
        :data="recordList"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="序号" align="center" type="index" width="150" />
        <el-table-column
          label="股道"
          prop="trackName"
          min-width="150"
          :show-overflow-tooltip="true"
        />
        <el-table-column
          label="列位"
          prop="complete"
          min-width="130"
          :show-overflow-tooltip="true"
        />
        <el-table-column
          label="车列号"
          prop="carCode"
          min-width="150"
          :show-overflow-tooltip="true"
        />
        <el-table-column
          label="班组"
          prop="workTeamName"
          min-width="150"
          :show-overflow-tooltip="true"
        />
        <el-table-column
          label="作业类型"
          align="center"
          prop="type"
          min-width="100"
          :show-overflow-tooltip="true"
        >
          <template slot-scope="scope">
            <div v-if="scope.row.type == '1'">供电预检</div>
            <div v-if="scope.row.type == '2'">断电作业</div>
            <div v-if="scope.row.type == '3'">供电检修</div>
          </template>
        </el-table-column>
        <el-table-column
          label="作业开始时间"
          align="center"
          prop="startTime"
          min-width="180"
          :show-overflow-tooltip="true"
        >
          <template slot-scope="scope">
            <span>{{ scope.row.startTime }}</span>
          </template>
        </el-table-column>

        <el-table-column
          label="作业结束时间"
          align="center"
          prop="endTime"
          min-width="180"
          :show-overflow-tooltip="true"
        >
          <template slot-scope="scope">
            <span>{{ scope.row.endTime }}</span>
          </template>
        </el-table-column>
        <el-table-column
          label="安全号志插设人"
          prop="insertPersonName"
          min-width="150"
          align="center"
          :show-overflow-tooltip="true"
        />
        <el-table-column
          label="安全号志撤除人"
          prop="removePersonName"
          min-width="150"
          align="center"
          :show-overflow-tooltip="true"
        />
        <el-table-column
          label="操作"
          align="center"
          class-name="small-padding fixed-width"
          fixed="right"
          width="200"
        >
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleUpdate(scope.row)"
              v-hasPermi="['record:record:edit']"
              >修改</el-button
            >
            <!-- <el-button
            size="mini"
            type="text"
             class="delBtn_R"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:record:remove']"
          >删除</el-button> -->
          </template>
        </el-table-column>
      </el-table>

      <pagination
        v-show="total > 0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </div>
    <!-- 添加或修改检修作业记录对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="700px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="车列号" prop="carCode">
          <el-input v-model="form.carCode" placeholder="请输入" disabled />
        </el-form-item>
        <el-form-item label="班组" prop="workTeamName">
          <el-input v-model="form.workTeamName" placeholder="请输入" disabled />
        </el-form-item>
        <el-form-item label="作业类型" prop="type">
          <el-input v-if="form.type == '1'" value="供电预检" disabled />
          <el-input v-if="form.type == '2'" value="断电作业" disabled />
          <el-input v-if="form.type == '3'" value="供电检修" disabled />
        </el-form-item>
        <el-form-item label="安全号志插设人" prop="insertPersonName">
          <el-input v-model="form.insertPersonName" placeholder="请输入" disabled />
        </el-form-item>
        <el-form-item label="作业开始时间" prop="startTime">
          <el-date-picker
            clearable
            v-model="form.startTime"
            type="datetime"
            format="yyyy-MM-dd HH:mm"
            value-format="yyyy-MM-dd HH:mm"
            placeholder="请选择作业开始时间"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="作业结束时间" prop="endTime">
          <el-date-picker
            clearable
            v-model="form.endTime"
            type="datetime"
            format="yyyy-MM-dd HH:mm"
            value-format="yyyy-MM-dd HH:mm"
            placeholder="请选择作业结束时间"
          >
          </el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listRecord,
  getRecord,
  delRecord,
  addRecord,
  updateRecord,
} from "@/api/record/record";

export default {
  name: "Record",
  data() {
    return {
      eleTabTop: 0, //表格容器eleTabTop距浏览器顶部距离
      types: [
        { id: 1, name: "供电预检" },
        { id: 2, name: "断电作业" },
        { id: 3, name: "供电检修" },
      ],
      dateRange: [],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 检修作业记录表格数据
      recordList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        carId: null,
        carCode: null,
        workPlanId: null,
        workPlanDetailId: null,
        workTeamId: null,
        insertPersonId: null,
        startTime: null,
        removePersonId: null,
        endTime: null,
        type: null,
        workTeamName: null,
        trackName: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        startTime: [{ required: true, message: "计划开始时间不能为空", trigger: "blur" }],
        endTime: [{ required: true, message: "计划结束时间不能为空", trigger: "blur" }],
        carId: [{ required: true, message: "车辆id不能为空", trigger: "blur" }],

        workTeamId: [{ required: true, message: "班组id不能为空", trigger: "blur" }],
      },
    };
  },
  created() {
    this.getList();
  },
  mounted() {
    // 滚动条的获取
    this.handleScrollx();
    window.addEventListener("scroll", this.handleScrollx, true);
  },
  activated() {
    //获取表格容器距页面头部的距离
    this.handleScrollx();
  },
  destroyed() {
    window.removeEventListener("scroll", this.handleScrollx, true);
  },
  methods: {
    //获取表格容器距页面头部的距离
    handleScrollx() {
      this.eleTabTop = this.$refs.eleTab.getBoundingClientRect().top;
    },
    /** 查询检修作业记录列表 */
    getList() {
      this.loading = true;
      listRecord(this.addDateRangeSearch(this.queryParams, this.dateRange)).then(
        (response) => {
          this.recordList = response.rows;
          this.total = response.total;
          this.loading = false;
        }
      );
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        carId: null,
        workPlanId: null,
        workPlanDetailId: null,
        workTeamId: null,
        insertPersonId: null,
        startTime: null,
        removePersonId: null,
        endTime: null,
        type: null,
        endTime: null,
        startTime: null,
        carCode:null,
        trackName: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.queryParams.type = undefined;
      this.dateRange = [];
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加检修作业记录";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getRecord(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改检修作业记录";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateRecord(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addRecord(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除检修作业记录编号为"' + ids + '"的数据项？')
        .then(function () {
          return delRecord(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "system/record/export",
        {
          ...this.queryParams,
        },
        `record_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
<style lang="scss" scoped>
::v-deep .eleTab {
  .el-table__header-wrapper {
    position: fixed;
    z-index: 9;
    top: 115px;
    right: 19px;
    padding-left: 238px;
  }
  // .el-table__fixed-header-wrapper .fixed-width {
  //     position: fixed;
  //     width: 0px  !important;
  //     top: 115px;
  //     height: 46px;
  //     right: 20px;

  // }
  .el-table__body-wrapper {
    margin-top: 50px !important;
  }
  .el-table__fixed-body-wrapper {
    z-index: 2;
    top: 50px !important;
  }
  .el-table__fixed-right {
    // z-index: 10;
  }
}

::v-deep.hideSidebar {
  .eleTab {
    .el-table__header-wrapper {
      padding-left: 249px !important;
    }
  }
}
</style>
